project(LogCollection)

set(SOURCES
    src/LogCollection.cpp
    src/LogCollectionPrivate.cpp
)

set(INCLUDES_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

add_library(LogCollector SHARED ${SOURCES})

# 查找 systemd 库
# 需要安装 PkgConfig库 sudo apt-get install pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(SYSTEMD REQUIRED libsystemd)

# 加入systemd的include目录
target_include_directories(LogCollector PRIVATE ${SYSTEMD_INCLUDE_DIRS})
# 链接systemd库
target_link_libraries(LogCollector PRIVATE ${SYSTEMD_LIBRARIES})


# 包含头文件路径
target_include_directories(LogCollector PUBLIC
    $<BUILD_INTERFACE:${INCLUDES_DIR}>  # 构建时用源码目录里的头文件
    $<INSTALL_INTERFACE:include>        # 安装后用安装目录下的 /usr/local/include/
)

# 安装.so到 /usr/local/lib
install(TARGETS LogCollector
    EXPORT LogCollectorTargets          # 导出 LogCollectorTargets ，存储 target使用信息
    DESTINATION lib                     # 安装位置 /usr/local/lib
)

# 安装头文件到 /usr/local/include
install(FILES
    ${INCLUDES_DIR}/LogCollection.hpp
    ${INCLUDES_DIR}/LogCollectionUtils.hpp
    DESTINATION include
)

# 安装默认配置文件到 /etc/log-collector
install(FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/LogConfigure.conf
    DESTINATION /etc/log-collector
)

# 安装导出的 CMake Targets, 让 find_package 能找到
install(EXPORT LogCollectorTargets      # 安装 LogCollectorTargets.cmake
    FILE LogCollectorTargets.cmake      # 确定Target的导出文件
    # NAMESPACE LogCollector::            # 加入命名空间
    DESTINATION lib/cmake/LogCollector  # 安装位置 /usr/local/lib/cmake/logCollector
)

# 引入辅助函数 -> configure_package_config_file
include(CMakePackageConfigHelpers)

# 生成 LogCollectorConfig.cmake, find_package 会用到
configure_package_config_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/LogCollectorConfig.cmake.in"   # 模板文件
    "${CMAKE_CURRENT_BINARY_DIR}/LogCollectorConfig.cmake"      # 输出文件
    INSTALL_DESTINATION lib/cmake/LogCollector                  # 安装路径
)

# 安装 LogCollectorConfig.cmake
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/LogCollectorConfig.cmake"
    DESTINATION lib/cmake/LogCollector
)